package com.example.demo.config;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextProvider implements ApplicationContextAware {

    private static volatile ApplicationContext context;

    public static void setContext(ApplicationContext context) {
        ApplicationContextProvider.context = context;
    }

    public static ApplicationContext getContext() {
        return context;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        setContext(applicationContext);
    }

    public static <T> T getBean(String name, Class<T> aClass) {
        return getContext().getBean(name, aClass);
    }

}
